<template>
  <div class="star" :class="starType">
    <span class="star-item" :class="itemClass" v-for="(itemClass,i) in itemClasses" :key="i"></span>
  </div>
</template>
<script>
const LENGTH = 5
const CLS_ON = 'on'
const CLS_OFF = 'off'
const CLS_HALF = 'half'
export default {
  props: {
    size: {
      type: Number,
      default: 0
    },
    score: {
      type: Number,
      default: 5.0
    }
  },
  computed: {
    starType() {
      return 'star-' + this.size
    },
    itemClasses() {
      var result = []
      var score = Math.floor(this.score * 2) / 2
      // 是否有小数
      var hasDecimal = score % 1 !== 0
      // 取整
      var integer = Math.floor(score)
      for (var i = 0; i < integer; i++) {
        result.push(CLS_ON)
      }
      if (hasDecimal) {
        result.push(CLS_HALF)
      }
      while (result.length < LENGTH) {
        result.push(CLS_OFF)
      }
      return result
    }
  }
}
</script>
<style lang="stylus" scoped>
@import '../../assets/stylus/mixin.styl';

.star {
  .star-item {
    display: inline-block;
    background-repeat: no-repeat;
  }

  &.star-48 {
    .star-item {
      width: 0.4rem;
      height: 0.4rem;
      background-size: 0.4rem;
      margin-right: 0.44rem;

      &:last-child {
        margin-right: 0;
      }

      &.on {
        color: red;
        bg-img('./star48_on');
      }

      &.half {
        bg-img('./star48_half');
      }

      &.off {
        bg-img('./star48_off');
      }
    }
  }

  &.star-36 {
    .star-item {
      width: 0.3rem;
      height: 0.3rem;
      background-size: 0.3rem;
      margin-right: 0.12rem;

      &:last-child {
        margin-right: 0;
      }

      &.on {
        bg-img('./star36_on');
      }

      &.half {
        bg-img('./star36_half');
      }

      &.off {
        bg-img('./star36_off');
      }
    }
  }

  &.star-24 {
    .star-item {
      width: 0.2rem;
      height: 0.2rem;
      background-size: 0.2rem;
      margin-right: 0.03rem;

      &:last-child {
        margin-right: 0;
      }

      &.on {
        bg-img('./star24_on');
      }

      &.half {
        bg-img('./star24_half');
      }

      &.off {
        bg-img('./star24_off');
      }
    }
  }
}
</style>
